package ee.ttu.rakarh.model.data.dao;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import ee.ttu.rakarh.model.data.entities.ItemType;
import ee.ttu.rakarh.util.HibernateUtil;

public class ItemTypeDAOImpl implements ItemTypeDAO {
public static final Logger LOG = Logger.getLogger(ItemTypeDAOImpl.class);
	@SuppressWarnings("unchecked")
	@Override
	public Collection<ItemType> getItemTypesByLevel(int lvl) {
		
		List<ItemType> itemTypes = new ArrayList<ItemType>();
		Session session = null;
		try {
			LOG.info("lvl=" + lvl);
			session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			Criteria crit = session.createCriteria(ItemType.class);
			itemTypes = crit.add(Restrictions.eq("level", lvl)).addOrder(Order.asc("typeName")).list();

		}
		catch (Exception e) {
			LOG.error(e);
		}finally{			
			//session.close();
		}
		return itemTypes;
	}

	@Override
	public ItemType getItemTypeById(int id) {

		ItemType itemType = null;
		Session session = null;
		try {
			LOG.info("id=" + id);
			session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			itemType = (ItemType) session.load(ItemType.class, id);

		}
		catch (Exception e) {
			LOG.error(e);
		}finally{			
			//session.close();
		}
		return itemType;
	}

}
